/**********************************************************
 * ImageServlet.java
 * Created by ankur
 * Last modified Date: Sep 8, 2008 4:26:23 PM by ankur
 * Version : 0.1
 * Copyright (c) 2008 - 2008 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.servlet;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.printeverywhere.delegator.BannerCampaignServiceDelegator;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.ItemsServiceDelegator;
import com.printeverywhere.delegator.ShopSettingDelegator;
import com.printeverywhere.dto.BannerCampaigns;
import com.printeverywhere.dto.ItemOffers;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.ShopSettings;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.CommonUtil;
import com.printeverywhere.util.SearchCriteria;

import static com.printeverywhere.util.AttributeConstants.*;

/**
 * This class is used to show images on JSP for ShopSettings
 * The Class ImageServlet.
 */
public class ImageServlet extends HttpServlet {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(ImageServlet.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 *
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 *
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 * Calls doPost
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (logger.isDebugEnabled()) {
			logger.debug("doGet(HttpServletRequest, HttpServletResponse) - start"); //$NON-NLS-1$
		}

		doPost(request, response);

		if (logger.isDebugEnabled()) {
			logger.debug("doGet(HttpServletRequest, HttpServletResponse) - end"); //$NON-NLS-1$
		}
	}

	/**
	 * Do post.
	 *
	 * @param request the request
	 * @param response the response
	 *
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 *
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if (logger.isDebugEnabled()) {
			logger.debug("doPost(HttpServletRequest, HttpServletResponse) - start"); //$NON-NLS-1$
		}

		byte[] photoData = null;
		String strDTOType = request.getParameter(DTO);
		String id = request.getParameter(ID);
		int intid=Integer.parseInt(id);
		if(strDTOType.equals(ITEMOFFERS) || strDTOType.equals(ITEMOFFERSDTO)){
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute(AdminConstants.ID, intid);
			ItemOffers itemOffers = (ItemOffers) ((CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemOffers.class)).size() >0 ? (CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ItemOffers.class)).get(0): null);
			if(itemOffers != null){
				photoData = itemOffers.getImage();
			}
		}else if (strDTOType.equals(ITEAM) || strDTOType.equals(ITEMSDTO)) {
			Items items = ItemsServiceDelegator.getItemsById(intid);
			if (items !=null) {
				photoData = items.getThumbImage();
			}
		} else if (strDTOType.equals(SHOP_SETTINGS)) {
			ShopSettings shop = ShopSettingDelegator.getShopSettings(intid);
			if (shop !=null) {
				photoData = shop.getLogo();
			}
		} else if (strDTOType.equals(BANNER_CAMPAIGNS)) {
			SearchCriteria searchCriteria = new SearchCriteria();
			searchCriteria.setAttribute("id", intid);
			List<BannerCampaigns> bannerList = (List<BannerCampaigns>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, BannerCampaigns.class);

			if(bannerList.size() > 0) {
				BannerCampaigns bannerCampaigns = bannerList.get(0);
				if (bannerCampaigns !=null) {
					photoData = bannerCampaigns.getImage();
				}
			}
		} else if (strDTOType.equals("path")) {
			File file = new File(System.getProperty("java.io.tmpdir") + "\\" + request.getParameter("name"));
			photoData = CommonUtil.getBytesFromFile(file);
		}


		response.setContentType(IMAGE);
		response.setContentLength(photoData.length);
		ServletOutputStream outputStream =  response.getOutputStream();
		outputStream.write(photoData);
		outputStream.flush();
		outputStream.close();

		if (logger.isDebugEnabled()) {
			logger.debug("doPost(HttpServletRequest, HttpServletResponse) - end"); //$NON-NLS-1$
		}
	}
}